package com.ming.closeto.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.ming.closeto.R;
import com.ming.closeto.base.BaseActivity;
import com.ming.closeto.entity.Constant;
import com.ming.closeto.model.MusicListModel;
import com.ming.closeto.model.MusicMoreModel;
import com.ming.closeto.utils.GlideUtils;
import com.ming.closeto.utils.OkHttpUtils;

import java.io.IOException;

public class MusicMoreActivity extends BaseActivity implements View.OnClickListener {

    private ImageView iv_cover;
    private TextView tv_title;
    private TextView tv_content;
    private TextView tv_lyric;
    private TextView tv_info;
    private TextView tv_charge_edt;

    private String title;
    private String id;

    private MusicMoreModel.DataBean bean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_more);
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        title = intent.getStringExtra("title");
        id = intent.getStringExtra("id");

        iv_cover = (ImageView) findViewById(R.id.iv_cover);
        iv_cover.setOnClickListener(this);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_content = (TextView) findViewById(R.id.tv_content);
        tv_lyric = (TextView) findViewById(R.id.tv_lyric);
        tv_info = (TextView) findViewById(R.id.tv_info);
        tv_charge_edt = (TextView) findViewById(R.id.tv_charge_edt);

        if (!TextUtils.isEmpty(title)) {
            getSupportActionBar().setTitle(title);
        }
        if (!TextUtils.isEmpty(id)) {
            initMusicMore();
        }
    }

    /**
     * 初始化音乐详情接口
     */
    private void initMusicMore() {
        OkHttpUtils.get(Constant.HTTP_MUSIC_MORE + id, new OkHttpUtils.OnOkHttpCallback() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onFailure(String msg) {

            }

            @Override
            public void onSuccessful(String json) {
                if (!TextUtils.isEmpty(json)) {
                    parsingMusicMoreJson(json);
                }
            }
        });
    }

    // 解析音乐详情的数据
    private void parsingMusicMoreJson(String json) {
        MusicMoreModel model = new Gson().fromJson(json, MusicMoreModel.class);
        bean = model.getData();

        GlideUtils.loadImageviewCenterCrop(this, bean.getCover(),iv_cover);
        tv_title.setText(bean.getStory_title());
        // HTML
        tv_content.setText(Html.fromHtml(bean.getStory()));
        tv_lyric.setText(bean.getLyric());
        tv_info.setText(bean.getInfo());
        tv_charge_edt.setText(bean.getCharge_edt());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_cover:
                if (bean != null) {
                    Intent intent = new Intent(this, WebViewActivity.class);
                    intent.putExtra("title", bean.getStory_title());
                    intent.putExtra("url", bean.getWeb_url());
                    startActivity(intent);
                }
                break;
        }
    }
}